package com.atguigu.chapter07.D04_State;

import com.atguigu.bean.WaterSensor;
import com.atguigu.util.AnqclnUtil;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.Comparator;
import java.util.List;

/**
 * Author: Pepsi
 * Date: 2023/8/9
 * Desc:
 */
public class Flink06_State_Key_Reduce {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 1000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        env
                .socketTextStream("hadoop101", 9999) // socket只能是1
                .map(line -> {
                    String[] data = line.split(",");
                    return new WaterSensor(
                            data[0],
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2])
                    );
                })
                .keyBy(WaterSensor::getId)
                // 键控状态必须在keyBy后使用
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {


                    private ReducingState<WaterSensor> vcSumState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        vcSumState = getRuntimeContext().getReducingState(
                                new ReducingStateDescriptor<WaterSensor>(
                                        "vcSumState",
                                        new ReduceFunction<WaterSensor>() {
                                            @Override
                                            public WaterSensor reduce(WaterSensor value1,
                                                                      WaterSensor value2) throws Exception {
                                                value1.setVc(value1.getVc() + value2.getVc());
                                                return value1;
                                            }
                                        },
                                        WaterSensor.class));
                    }

                    @Override
                    public void processElement(WaterSensor value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {
                        // 将传过来的每个元素加入到状态里面去，然后就行了，他会自己聚合，因为在上面创建状态的时候就已经写了聚合的逻辑
                        vcSumState.add(value);
                        out.collect(ctx.getCurrentKey()+" 的水位和为："+vcSumState.get().getVc());
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
